package com.niiwoo.civet.trade.service.local.loan;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.dao.entity.ProjectPreSubmitRecord;
import com.niiwoo.civet.trade.dao.entity.ProjectRateConfig;
import com.niiwoo.civet.trade.dao.mapper.ProjectPreSubmitRecordMapperExt;
import com.niiwoo.civet.trade.dto.request.loan.LoanPreSubmitRecordSaveReqDTO;
import com.niiwoo.civet.trade.enums.ProjectTypeEnum;
import com.niiwoo.civet.user.dto.response.UserBasicInfoResponseDTO;
import com.niiwoo.civet.user.dto.response.UserIdentityInfoResponseDTO;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * @Author: wuke
 * @Description:
 * @Date: Created in 20:57 2018/4/16
 */
@Slf4j
@Service
public class LoanPreSubmitRecordService {
    @Autowired
    private ProjectPreSubmitRecordMapperExt projectPreSubmitRecordMapper;
    @Autowired
    protected LoanQueryService loanQueryService;
    @Autowired
    protected LoanConfigService loanConfigService;
    @Autowired
    protected PrivacyMasks privacyMasks;
    @Autowired
    protected SnowflakeIdWorker snowflakeIdWorker;

    public void saveLoanPreSubmitRecord(LoanPreSubmitRecordSaveReqDTO reqDTO){
        log.info("saveLoanPreSubmitRecord.reqDTO={}", JSON.toJSONString(reqDTO));

        String userId = reqDTO.getUserId();
        ProjectTypeEnum projectTypeEnum = reqDTO.getProjectTypeEnum();
        Integer deadline = reqDTO.getDeadline();
        Date now = new Date();
        String submitTime = FastDateFormat.getInstance("yyyy-MM-dd").format(now);
        int existCount = projectPreSubmitRecordMapper.selectByUserIdTypeSubmitTime(userId, projectTypeEnum, submitTime);

        log.info("saveLoanPreSubmitRecord.userId={},existCount={}",userId,existCount);

        if(existCount>0){
            return;
        }

        //获取用户基本信息
        UserBasicInfoResponseDTO userBasicInfo = loanQueryService.getUserBasicInfo(userId);
        //获取用户身份证信息
        UserIdentityInfoResponseDTO userIdentityInfo = loanQueryService.getUserIdentityInfo(userId);
        ProjectRateConfig projectRateConfig = loanConfigService.selectRateConfigByDeadlineTypeEnum(deadline,projectTypeEnum);

        ProjectPreSubmitRecord record = new ProjectPreSubmitRecord();
        record.setId(snowflakeIdWorker.nextId());
        record.setUserId(userId);
        record.setType(projectTypeEnum.getValue());
        record.setSubmitTime(submitTime);
        record.setBorrowUserName(userIdentityInfo==null?null:userIdentityInfo.getName());
        record.setBorrowMobile(this.ecryptString(userBasicInfo.getMobileNo()));
        record.setTitle(reqDTO.getLoanTitle());
        record.setAmount(reqDTO.getLoanAmount());
        record.setBorrowRate(projectRateConfig.getBorrowInterestRate());
        record.setDeadline(reqDTO.getDeadline());
        record.setCreateTime(now);
        record.setUpdateTime(now);
        projectPreSubmitRecordMapper.insertSelective(record);
    }

    //加密字符串
    private String ecryptString(String str) {
        return Optional.ofNullable(str).map(s->{
            return privacyMasks.encryptPrivacy(str);
        }).orElse(null);
    }
}
